package cn.lili.event.impl;

import cn.lili.common.utils.CurrencyUtil;
import cn.lili.event.GoodsCommentCompleteEvent;
import cn.lili.event.UserRegisterEvent;
import cn.lili.event.OrderStatusChangeEvent;
import cn.lili.modules.member.client.UserClient;
import cn.lili.modules.member.entity.dos.MemberEvaluation;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.order.order.client.OrderClient;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dto.OrderMessage;
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
import cn.lili.modules.payment.client.WalletPointClient;
import cn.lili.modules.system.client.SettingClient;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.ExperienceSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;

/**
 * 会员经验值
 *
 * @author Bulbasaur
 * @since 2021/5/16 11:16 下午
 */
@RequiredArgsConstructor
// @Service
public class UserExperienceExecute
        implements UserRegisterEvent, GoodsCommentCompleteEvent, OrderStatusChangeEvent {

    /**
     * 配置
     */
    private final SettingClient settingClient;
    /**
     * 会员
     */
    private final UserClient userClient;
    /**
     * 订单
     */
    private final OrderClient orderClient;
    private final WalletPointClient walletPointClient;

    /**
     * 会员注册赠送经验值
     *
     * @param member 会员
     */
    @Override
    public void userRegister(User member) {
        // 获取经验值设置
        ExperienceSetting experienceSetting = getExperienceSetting();
        // 赠送会员经验值
//        walletPointClient.updateMemberPoint(
//                UserPointUpdateDTO.builder()
//                        .userId(member.getId())
//                        .points(experienceSetting.getRegister().longValue())
//                        .userPointServiceEnum(UserPointServiceEnum.SIGN)
//                        .description("会员注册，赠送经验值" + experienceSetting.getRegister()).build());
    }

    /**
     * 商品评价赠送经验值
     *
     * @param memberEvaluation 会员评价
     */
    @Override
    public void goodsComment(MemberEvaluation memberEvaluation) {
        // 获取经验值设置
        ExperienceSetting experienceSetting = getExperienceSetting();
        // 赠送会员经验值
//        walletPointClient.updateMemberPoint(
//                UserPointUpdateDTO.builder()
//                        .userId(memberEvaluation.getMemberId())
//                        .points(experienceSetting.getComment().longValue())
//                        .userPointServiceEnum(UserPointServiceEnum.FULL_DISCOUNT_GIFT)
//                        .description("会员评价，赠送经验值" + experienceSetting.getComment()).build()
//                );
    }

    /**
     * 完成订单赠送经验值
     *
     * @param orderMessage 订单消息
     */
    @Override
    public void orderChange(OrderMessage orderMessage) {
        if (orderMessage.getNewStatus().equals(OrderStatusEnum.COMPLETED)) {
            // 获取经验值设置
            ExperienceSetting experienceSetting = getExperienceSetting();
            // 获取订单信息
            Order order = orderClient.getBySn(orderMessage.getOrderSn());
            // 计算赠送经验值数量
            Double point = CurrencyUtil.mul(experienceSetting.getMoney(), order.getFlowPrice(), 0);
            // 赠送会员经验值
//            walletPointClient.updateMemberPoint(
//                    UserPointUpdateDTO.builder()
//                            .userId(order.getMemberId())
//                            .points(point.longValue())
//                            .userPointServiceEnum(UserPointServiceEnum.FULL_DISCOUNT_GIFT)
//                            .description("会员下单，赠送经验值" + point + "分").build());
        }
    }

    /**
     * 获取经验值设置
     *
     * @return 经验值设置
     */
    private ExperienceSetting getExperienceSetting() {
        Setting setting = settingClient.get(SettingEnum.EXPERIENCE_SETTING.name());
        return new Gson().fromJson(setting.getSettingValue(), ExperienceSetting.class);
    }
}
